% FUNCTION: fdescent_06
%
% Calculates the rates of change of aircraft state
% variables during an aircraft descent profile

% programmer: A. Trani
% date: Sept. 17, 2006

% Calls: FUNCTION  densityAltitudeoffISA.m (similar to isam.m)
%                                drag_03.m
%                                thrust_calculation.m


function [yprime] = fdescent_06(t,y)

global  g tsfc macht Cdoct thrust_table mach_table lapse_rate_factor 
global rhos Vdescent altc hcruise deltaTemp

V = interp1 (altc,Vdescent,y(1));  			     % speed schedule - speed is calibrated airspeed (CAS) or indicated airspeed (IAS)

% Computes standard atmosphere values
% true Mach number, speed of sound (m/s) at altitude, density (kg/cu.m), temperature (deg.
% Kelvin and density altitude (dAlt)

[mtrue,a,rho,temp,dAlt] = densityAltitudeoffISA(deltaTemp,y(1),V);

vtas = mtrue * a;                        % true airspeed (m/s)
q = 0.5 * rho * vtas^2;               % dynamic pressure (N)
phi = 0.0;                                  % bank angle (radians) if maneuvering flight is simulated
	  
% Compute drag and L/D ratio (call drag_new)
%[drag,LD,cl,cd]=drag_03(mtrue,rho,a,y(2)/g,macht,Cdoct,phi);
[drag,LD,cl,cd]=drag_03(mtrue,rho,a,y(2)/g,macht,Cdoct,phi);

% Estimates the thrust during descent. Assumed to be a small percent of the
% maximum continuous thrust. tResidual is the percent of continuous thrust
% provided under idle engine conditions

tResidual = 1/10;            % residual thrust (dim) during the descent profile

[thrust] = thrust_calculationNoLoss(thrust_table,mach_table,lapse_rate_factor,mtrue,rho)*tResidual;

% Define the rate equations (4 rate equations for 4 state variables)
% y(1) - Aircraft altitude (m)
% y(2) - Aircraft weight (N)
% y(3) - Distance traveled along the path (m)
% y(4) - Distance traveled wrt to ground (m)

yprime(1) = vtas * (thrust - drag) / (y(2)); 		 % Rate of descent (m/s)
yprime(2) = - tsfc * thrust;								% Mass flow rate (N/s) 
yprime(3) = vtas ;											   % Distance traveled along the flight path (m)

% Calculation of flight path angle (gamma)

flt_path_angle = asin(yprime(1)/ vtas);     % radians

% Calculate distance traveled across horizontal plane (flat Earth)

yprime(4) = vtas * cos(flt_path_angle);     % distance traveled along horizontal (meters)

yprime = yprime'; % transposes the array going out
															% MATLAB is very picky about this
															% it likes the array in column format
                                                            
                                           
